package puppy.graph;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.regex.Pattern;

import util.io.FileInput;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import fileloaders.parsers.InLineParser;

public class CreateLM   {

	private static final Integer EDGE_THRESHOLD = 0;
	
	
	private int _id_a = 0;
	private int _id_b = 1;
	private int _tagA = 1;
	private int _tagB = 2;
	private int _freq_b= 5;
	private int _freq_a = 4;
	private int _freq_ab = 3;

	private int _age = 0;
			
	//  ids-teens-mteens	1	7	1	0.0
	private int _adult_freq_a= 7;
	private int _adult_freq_b=8;
	private int _adult_freq_ab=4;
	
	
	private Pattern tab = Pattern.compile("\t");
	
	//public Hashtable<Integer,Hashtable<Integer,Short>> lm = new Hashtable<Integer,Hashtable<Integer,Short>>();
//	public Hashtable<String,Hashtable<String,Short>> lm = new Hashtable<String,Hashtable<String,Short>>();
	public  Hashtable<String,Hashtable<String, DeliciousEdgeSimple >> lm =  new Hashtable<String,Hashtable<String, DeliciousEdgeSimple >> ();
	
	public CreateLM(){
		
		
	}
	
	//kids kids-teens teens teens-mteens mteesn all adults
	
	int j =0;
	public void initLM(String path) {

		FileInput in = new FileInput(path);
		
		String line = in.readString();
		
		
		while (line != null) {
		//	System.out.println("Adding " + line);
			dumpLineIntoGraphDirected(lm, line);
			line = in.readString();
			
	//	System.out.println(j);
	//	if(j==5) return;
		}
	}

	
	
	
	private void dumpLineIntoGraphDirected(
			 Hashtable<String,Hashtable<String, DeliciousEdgeSimple >> lm,
			String line) {

		
			String[] fields = tab.split(line);

			
			Hashtable<String, Object> elements = InLineParser.parseBigramEnrichedLine(fields);
		
			if(elements ==null) {
				return;
			}
			
			String [] keys = (String[]) elements.get("keys");
			elements.remove("keys");
			DeliciousEdgeSimple edge= new DeliciousEdgeSimple();
			Enumeration<String> ages = elements.keys();
			
		
			String a=  keys[0];
			String b=  keys[1];
			
			while(ages.hasMoreElements()){
				String age = ages.nextElement();
				edge.freq.put(age, (Integer)elements.get(age));
				
			}
			
			
			DeliciousNode tag = null;
	
			
			if (a.compareTo(b) < 0) {
		//	System.out.println("aqui");
				if(lm.containsKey(a)){
					Hashtable<String, DeliciousEdgeSimple> temp = lm.get(a);
					
					temp.put(b, edge);
					lm.put(a,temp);
					
				}else{
					
					Hashtable<String, DeliciousEdgeSimple> temp = new Hashtable<String,DeliciousEdgeSimple>();
					
					temp.put(b,edge);
					
					lm.put(a,temp);
				}
				
			}else{
				
				if(lm.containsKey(b)){
					Hashtable<String, DeliciousEdgeSimple> temp = lm.get(b);
					
					temp.put(a, edge);
					lm.put(b,temp);
					
				}else{
					
					Hashtable<String, DeliciousEdgeSimple> temp = new Hashtable<String,DeliciousEdgeSimple>();
					
					temp.put(a,edge);
					
					lm.put(b,temp);
				}
				
				
				
				
			
		}
	}


	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//String path = "/media/sata__/data/delicious_graph/delicious_graphML_raw_ids_dmoz.txt";
		String path = prefix+ "delicious_graphML_raw_ids_dmoz_youth_clean_kids.txt";
		
	//String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//prefix = "/home/sergio/data/";
		 path = prefix + "delicious_graphML_node_node_mix_kidsteens_simple.txt";
		
		
		
		CreateLM graph = new CreateLM();


		graph.initLM(path);
		
		     
	
	}

}
